﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common.ValueObject;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Tool.UserControls;

namespace ERP_RORZE_TOOL.Tool.Forms
{
    public partial class frm_ToolAdd : DevExpress.XtraEditors.XtraForm
    {
        private ucTool parent;
        public frm_ToolAdd(ucTool fTool)
        {
            InitializeComponent();
            btAdd.Image = ERP_RORZE_TOOL.Properties.Resources.add;
            btCancel.Image = ERP_RORZE_TOOL.Properties.Resources.cancel;
            parent = fTool;
            this.CancelButton = this.btCancel;
            this.ActiveControl = txtToolID;

        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            ToolHelper helper = new ToolHelper(new Tools());
            if (GetData(helper.ValueObject))
            {


                if (helper.Insert() > 0)
                {

                    if (Utilities.MessageConfirmationYesNo(ToolConstants.Message.AddSuccessAndQuestion))
                    {

                        txtToolID.Text = "";
                        txtToolName.Text = "";
                        txtMakerToolname.Text = "";
                        txtPrice.Text = "";
                        txtSafeQty.Text = "";
                        cmbMaker.SelectedItem = cmbMaker.Items[0];
                        cmbFrequency.SelectedItem = cmbFrequency.Items[0];
                        cmbToolGroup.SelectedItem = cmbToolGroup.Items[0];
                        txtToolID.Focus();
                    }
                    else
                    {
                        this.Close();
                    }
                }
                else
                {

                    Utilities.MessageErrorOk(ToolConstants.Message.AddFailed);
                }
                parent._action = 0;
                parent.BindDatatoGrid();
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_ToolAdd_Load(object sender, EventArgs e)
        {
            BindDataToFrequency();
            BindDataToToolGroup();
            BindDataMaker();
        }

        #region Private methods

        private bool CheckDuplicate(string ToolId)
        {
            ToolHelper helper = new ToolHelper(new Tools());
            helper.ValueObject.ToolID = ToolId;
            DataTable dt = helper.GetOne();
            if (dt.Rows != null && dt.Rows.Count > 0)
                return true;
            return false;

        }

        private bool GetData(Tools o)
        {
            #region Check valid

            if (Utilities.IsNullOrEmpty(txtToolID.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtToolName.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.ToolNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtMakerToolname.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.MakerToolNameCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbFrequency.SelectedValue) || cmbFrequency.SelectedItem == cmbFrequency.Items[0])
            {
                MessageBox.Show(ToolConstants.Message.Tool.FrequencyCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(cmbToolGroup.SelectedValue) || cmbToolGroup.SelectedItem == cmbToolGroup.Items[0])
            {
                MessageBox.Show(ToolConstants.Message.Tool.ToolGroupCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtPrice.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.PriceCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (decimal.Parse(txtPrice.Text.Trim()) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.Tool.PriceMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (Utilities.IsNullOrEmpty(txtSafeQty.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.SafeQtyCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            //if (int.Parse(txtSafeQty.Text.Trim()) <= 0)
            //{
            //    MessageBox.Show(ToolConstants.Message.Tool.SafeQtyMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    return false;
            //}

            if (CheckDuplicate(txtToolID.Text.Trim()))
            {
                MessageBox.Show(ToolConstants.Message.Tool.ToolHasExisted, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            #endregion

            o.ToolID = txtToolID.Text;
            o.ToolName = txtToolName.Text;
            o.MakerToolName = txtMakerToolname.Text;
            o.Price = decimal.Parse(txtPrice.Text);
            o.SafeQty = int.Parse(txtSafeQty.Text);
            o.FrequencyID = cmbFrequency.SelectedValue.ToString();
            o.ToolGroupID = cmbToolGroup.SelectedValue.ToString();
            o.MakerID = cmbMaker.SelectedValue.ToString();

            return true;
        }

        private void BindDataToFrequency()
        {
            FrequencyHelper helper = new FrequencyHelper(new Frequency());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            cmbFrequency.DataSource = dt;
            cmbFrequency.DisplayMember = ToolConstants.Entities.Frequency.FieldName.FullName;
            cmbFrequency.ValueMember = ToolConstants.Entities.Frequency.FieldName.FrequencyID;
            row[ToolConstants.Entities.Frequency.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Frequency.FieldName.FrequencyID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbFrequency.SelectedItem = cmbFrequency.Items[0];

        }

        private void BindDataToToolGroup()
        {
            ToolGroupHelper helper = new ToolGroupHelper(new ToolGroup());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            cmbToolGroup.DataSource = dt;
            cmbToolGroup.DisplayMember = ToolConstants.Entities.ToolGroup.FieldName.FullName;
            cmbToolGroup.ValueMember = ToolConstants.Entities.ToolGroup.FieldName.ToolGroupID;
            row[ToolConstants.Entities.ToolGroup.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.ToolGroup.FieldName.ToolGroupID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbToolGroup.SelectedItem = cmbToolGroup.Items[0];

        }

        #endregion

        protected override bool ProcessKeyPreview(ref Message m)
        {
            if (m.Msg == 0x0100 && (int)m.WParam == 13)
            {
                this.ProcessTabKey(true);
            }
            return base.ProcessKeyPreview(ref m);
        }

        private void BindDataMaker()
        {
            MakerHelper helper = new MakerHelper(new Maker());
            DataTable dt = helper.GetAll();
            DataRow row = dt.NewRow();

            row[ToolConstants.Entities.Maker.FieldName.FullName] = "Select...";
            row[ToolConstants.Entities.Maker.FieldName.MakerID] = null;
            dt.Rows.InsertAt(row, 0);

            cmbMaker.DataSource = dt;
            cmbMaker.DisplayMember = ToolConstants.Entities.Maker.FieldName.FullName;
            cmbMaker.ValueMember = ToolConstants.Entities.Maker.FieldName.MakerID;

            cmbMaker.SelectedItem = cmbMaker.Items[0];
        }

    }
}